package com.codedancer.campus.manager.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codedancer.campus.common.exception.BusinessException;
import com.codedancer.campus.manager.service.IConfessionalWallCommentService;
import com.codedancer.campus.manager.service.bo.confessionalWallComment.ConfessionalWallCommentPageSearchBo;
import com.codedancer.campus.manager.service.vo.confessionalWallComment.ConfessionalWallCommentVo;
import com.codedancer.campus.mapper.ConfessionalWallCommentMapper;
import com.codedancer.campus.mapper.UserMapper;
import com.codedancer.campus.po.ConfessionalWallComment;
import com.codedancer.campus.po.User;
import com.codedancer.campus.qo.confessionalWallComment.ConfessionalWallCommentPageSearchQo;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class ConfessionalWallCommentServiceImpl implements IConfessionalWallCommentService {

    @Autowired
    private ConfessionalWallCommentMapper confessionalWallCommentMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 分页查询表白墙父级评论
     * @param confessionalWallCommentPageSearchBo
     * @return
     */
    @Override
    public IPage<ConfessionalWallCommentVo> pageParentComment(
        ConfessionalWallCommentPageSearchBo confessionalWallCommentPageSearchBo) {
        ConfessionalWallCommentPageSearchQo confessionalWallCommentPageSearchQo = BeanUtil
            .copyProperties(confessionalWallCommentPageSearchBo,
                ConfessionalWallCommentPageSearchQo.class);
        IPage<ConfessionalWallComment> confessionalWallCommentIPage = confessionalWallCommentMapper
            .pageParentComment(
                new Page<ConfessionalWallComment>(confessionalWallCommentPageSearchBo.getPage(),
                    confessionalWallCommentPageSearchBo.getSize()),
                confessionalWallCommentPageSearchQo);
        IPage<ConfessionalWallCommentVo> confessionalWallCommentVoIPage = new Page<>();
        List<ConfessionalWallCommentVo> confessionalWallCommentVos;
        confessionalWallCommentVos = confessionalWallCommentIPage.getRecords().stream()
            .map(confessionalWallComment -> {
                ConfessionalWallCommentVo confessionalWallCommentVo = BeanUtil
                    .copyProperties(confessionalWallComment, ConfessionalWallCommentVo.class);
                confessionalWallCommentVo.setCommentTime(confessionalWallComment.getCreateTime());
                User user = userMapper.selectById(confessionalWallComment.getCommentUserUuid());
                confessionalWallCommentVo.setCommentNickName(user.getNickName());
                confessionalWallCommentVo.setCommentAvatar(user.getAvatar());
                // 判断父级评论是否有儿子
                confessionalWallCommentVo.setHasChildren(
                    confessionalWallCommentMapper.hasChildren(confessionalWallComment.getUuid()));
                return confessionalWallCommentVo;
            }).collect(Collectors.toList());
        confessionalWallCommentVoIPage.setRecords(confessionalWallCommentVos);
        confessionalWallCommentVoIPage.setTotal(confessionalWallCommentIPage.getTotal());
        return confessionalWallCommentVoIPage;
    }

    /**
     * 分页查询该评论下子集评论
     * @param confessionalWallCommentPageSearchBo
     * @return
     */
    @Override
    public IPage<ConfessionalWallCommentVo> pageChildrenComment(
        ConfessionalWallCommentPageSearchBo confessionalWallCommentPageSearchBo) {
        ConfessionalWallCommentPageSearchQo confessionalWallCommentPageSearchQo = BeanUtil.copyProperties(
            confessionalWallCommentPageSearchBo, ConfessionalWallCommentPageSearchQo.class);
        IPage<ConfessionalWallComment> confessionalWallCommentIPage = confessionalWallCommentMapper.pageChildrenComment(
            new Page<ConfessionalWallComment>(confessionalWallCommentPageSearchBo.getPage(),
                confessionalWallCommentPageSearchBo.getSize()),
            confessionalWallCommentPageSearchQo);
        IPage<ConfessionalWallCommentVo> confessionalWallCommentVoIPage = new Page<>();
        List<ConfessionalWallCommentVo> confessionalWallCommentVos;
        confessionalWallCommentVos = confessionalWallCommentIPage.getRecords().stream()
            .map(confessionalWallComment -> {
                ConfessionalWallCommentVo confessionalWallCommentVo = BeanUtil
                    .copyProperties(confessionalWallComment, ConfessionalWallCommentVo.class);
                confessionalWallCommentVo.setCommentTime(confessionalWallComment.getCreateTime());
                User user = userMapper.selectById(confessionalWallComment.getCommentUserUuid());
                confessionalWallCommentVo.setCommentNickName(user.getNickName());
                confessionalWallCommentVo.setCommentAvatar(user.getAvatar());
                // 判断父级评论是否有儿子
                confessionalWallCommentVo.setHasChildren(
                    confessionalWallCommentMapper.hasChildren(confessionalWallComment.getUuid()));
                // 查询被回复用户
                User repliedUser = userMapper
                    .selectById(confessionalWallComment.getRepliedUserUuid());
                confessionalWallCommentVo.setRepliedNickName(repliedUser.getNickName());
                return confessionalWallCommentVo;
            }).collect(Collectors.toList());
        confessionalWallCommentVoIPage.setRecords(confessionalWallCommentVos);
        confessionalWallCommentVoIPage.setTotal(confessionalWallCommentIPage.getTotal());
        confessionalWallCommentVoIPage.setPages(confessionalWallCommentIPage.getPages());
        confessionalWallCommentVoIPage.setSize(confessionalWallCommentPageSearchBo.getSize());
        return confessionalWallCommentVoIPage;
    }

    /**
     * 删除评论
     *
     * @param uuid
     */
    @Override
    public void delete(String uuid) {
        ConfessionalWallComment confessionalWallComment = confessionalWallCommentMapper
            .findById(uuid).orElseThrow(() -> new BusinessException("评论不存在"));
        // 判断是否是顶级评论
        if (confessionalWallComment.getParentUuid().equals("0")) {
            // 先删除子集评论
            confessionalWallCommentMapper
                .deleteByColumn("parent_uuid", confessionalWallComment.getUuid());
        }
        // 再删除该条评论
        confessionalWallCommentMapper.deleteById(uuid);
    }
}
